<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入标题"
          clearable
          style="width: 140px;"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="文章来源" prop="articleOrigin" v-if="allowedAll">
        <el-input
          v-model="queryParams.articleOrigin"
          placeholder="请输入文章来源"
          clearable
          style="width: 140px;"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="栏目名称" prop="columnId">
        <el-select
          clearable
          filterable
          style="width: 140px;"
          v-model="queryParams.columnId" placeholder="请选择">
          <el-option
            placeholder="请选择"
            v-for="item in columnOptions"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="责任编辑" prop="author" v-if="allowedAll">
        <el-input
          v-model="queryParams.author"
          placeholder="请输入责任编辑"
          clearable
          style="width: 140px;"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="审核状态" prop="state" v-if="!allowedAll">
        <el-select v-model="queryParams.state" placeholder="请选择" clearable style="width: 140px;">
          <el-option
            v-for="dict in dict.type.check"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="回收数据类型" prop="type" v-if="allowedAll">
        <el-select v-model="queryParams.type" placeholder="请选择" clearable style="width: 140px;">
          <el-option
            v-for="dict in dict.type.recover_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['station:station:add']"
        >新增</el-button>
      </el-col> -->
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['station:station:edit']"
        >修改</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          size="mini"
          :disabled="multiple"
          @click="handleRecover"
        >恢复数据</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['station:station:remove']"
        >彻底删除</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['station:station:export']"
        >导出</el-button>
      </el-col> -->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="stationList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="50" align="center" />
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="标题" align="center" prop="title" >
        <template slot-scope="scope">
          {{ $stripHtml(scope.row.title) }}
        </template>
      </el-table-column>>
      <el-table-column label="文章来源" align="center" prop="articleOrigin" />
      <el-table-column label="栏目名称" align="center" prop="columnName" />
      <!-- <el-table-column label="内容" align="center" prop="content" /> -->
      <el-table-column label="内容" align="center" prop="content" width="300">
        <template slot-scope="scope">
          <div class="content-cell">{{ formatContent(scope.row.content) }}</div>
        </template>
      </el-table-column>
      <el-table-column label="责任编辑" align="center" prop="author" />
      <el-table-column label="备注" align="center" prop="remark" />
      <!-- <el-table-column label="封面" align="center" prop="coverPath" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.coverPath" :width="50" :height="50"/>
        </template>
      </el-table-column> -->
      <el-table-column label="审批表" align="center" prop="appval" width="100">
          <template slot-scope="scope">
            <image-preview :src="scope.row.appval" :width="50" :height="50"/>
          </template>
        </el-table-column>
      <el-table-column label="审核状态" align="center" prop="state" v-if="!allowedAll">
        <template slot-scope="scope">
          <el-tag size="medium" :type="scope.row.state==0?'danger':scope.row.state==1?'success':'warning'">
            <dict-tag :options="dict.type.check" :value="scope.row.state"/>
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="回收数据类型" align="center" prop="type" v-if="allowedAll">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.recover_type" :value="scope.row.type"/>
        </template>
      </el-table-column>
      <!-- 有效日期 -->
      <el-table-column label="有效期" align="center" prop="validDate">
        <template slot-scope="scope">
          <span style="color: red;">{{ scope.row.validDate}}天</span>
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['station:station:edit']"
          >修改</el-button> -->
          <el-button
            size="mini"
            type="text"
            @click="handleRecover(scope.row)"
          >恢复数据</el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleDelete(scope.row)"
            v-hasPermi="['station:station:remove']"
          >彻底删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改回收站对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="标题" prop="title">
          <el-input v-model="form.title" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="文章来源" prop="articleOrigin">
          <el-input v-model="form.articleOrigin" placeholder="请输入文章来源" />
        </el-form-item>
        <el-form-item label="栏目id" prop="columnId">
          <el-input v-model="form.columnId" placeholder="请输入栏目id" />
        </el-form-item>
        <el-form-item label="内容">
          <editor v-model="form.content" :min-height="192"/>
        </el-form-item>
        <el-form-item label="责任编辑" prop="author">
          <el-input v-model="form.author" placeholder="请输入责任编辑" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
        <!-- <el-table-column label="封面" align="center" prop="coverPath" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.coverPath" :width="50" :height="50"/>
        </template>
        </el-table-column> -->
        <el-form-item label="author对应的登录ip地址" prop="ip">
          <el-input v-model="form.ip" placeholder="请输入author对应的登录ip地址" />
        </el-form-item>
        <el-form-item label="发布时间" prop="pubdate">
          <el-date-picker clearable
            v-model="form.pubdate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择发布时间">
          </el-date-picker>
        </el-form-item>
        <el-table-column label="审批表" align="center" prop="appval" width="100">
          <template slot-scope="scope">
            <image-preview :src="scope.row.appval" :width="50" :height="50"/>
          </template>
        </el-table-column>
        <el-form-item label="审核id" prop="auditId">
          <el-input v-model="form.auditId" placeholder="请输入审核id" />
        </el-form-item>
        <el-form-item label="删除的已审核文章表id" prop="articleId">
          <el-input v-model="form.articleId" placeholder="请输入删除的已审核文章表id" />
        </el-form-item>
        <el-form-item label="删除的审核文章表id" prop="uncheckId">
          <el-input v-model="form.uncheckId" placeholder="请输入删除的审核文章表id" />
        </el-form-item>
        <el-form-item label="审核状态" prop="state">
          <el-input v-model="form.state" placeholder="请输入审核状态" />
        </el-form-item>
        <el-form-item label="回收站数据类型-0:uncheck表回收，1:已审核文章表回收" prop="type">
          <el-select v-model="form.type" placeholder="请选择回收站数据类型-0:uncheck表回收，1:已审核文章表回收">
            <el-option
              v-for="dict in dict.type.recover_type"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="有效期(天)" prop="validDate">
          <el-input-number v-model="form.validDate" controls-position="right" :min="0" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listStation, getStation, delStation, addStation, updateStation } from "@/api/station/station";
import {listConlumnList, listConlumnListByRole} from "@/api/conlumn/conlumn";
import {recoverData} from "@/api/station/station";
import EnhancedEditor from "@/components/Editor/EnhancedEditor";

export default {
  name: "Station",
  dicts: ['recover_type', 'check'],
  components: {
    EnhancedEditor
  },
  data() {
    return {
      allowedAll: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 回收站表格数据
      stationList: [],
      //栏目名称列表数据
      columnOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: null,
        articleOrigin: null,
        columnId: null,
        content: null,
        author: null,
        type: null,
        state: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        title: [
          { required: true, message: "标题不能为空", trigger: "blur" }
        ],
        columnId: [
          { required: true, message: "栏目id不能为空", trigger: "blur" }
        ],
        content: [
          { required: true, message: "内容不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {

    handleRecover(row) {
      const id = row.id || this.ids;
      this.$modal.confirm('确定要恢复吗？')
        .then(() => {
          return recoverData(id);
        })
        .then(response => {
          let message = response.msg;
          console.log(message);
          this.$modal.msgSuccess(message);
          this.getList();
        })
        .catch(error => {
          console.error("恢复失败:", error);
          this.$modal.msgError("恢复失败");
        });
    },
    formatContent(content) {
      // 先移除HTML标签
      const plainText = content.replace(/<[^>]*>/g, '');
      // 截取前100个字符（可根据需要调整）
      return plainText.length > 100 ? plainText.substring(0, 100) + '...' : plainText;
    },
    /** 查询栏目列表 */
    getColumnList() {
      // 根据用户权限选择不同的API方法
      if (this.allowedAll) {
        // 市局用户使用全部栏目列表
        listConlumnList().then(response => {
          this.columnOptions = response.rows;
        });
      } else {
        // 分局用户使用带权限控制的栏目列表
        listConlumnListByRole().then(response => {
          this.columnOptions = response.rows;
        });
      }
    },
    /** 查询回收站列表 */
    getList() {
      this.loading = true;
      listStation(this.queryParams).then(response => {
        const {rows,total} = response.data;
        this.allowedAll = response.allowedAll;
        this.stationList = rows.map(item=>{
          const column = this.columnOptions.find(col => col.id === item.columnId);
          return{
            ...item,
            columnName: column ? column.name : '未知栏目'
          }
        });
        this.total = total;
        this.loading = false;
        // 重新获取栏目列表以确保权限正确
        this.getColumnList();
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        title: null,
        articleOrigin: null,
        columnId: null,
        content: null,
        author: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null,
        remark: null,
        coverPath: null,
        ip: null,
        pubdate: null,
        appval: null,
        auditId: null,
        articleId: null,
        uncheckId: null,
        state: null,
        type: null,
        validDate: 30
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加回收站";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getStation(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改回收站";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateStation(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addStation(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除回收站编号为"' + ids + '"的数据项？').then(function() {
        return delStation(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('station/station/export', {
        ...this.queryParams
      }, `station_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style scoped>
.content-cell {
  display: -webkit-box;
  -webkit-line-clamp: 2; /* 限制显示两行 */
  -webkit-box-orient: vertical;
  overflow: hidden;
  text-overflow: ellipsis;
  line-height: 1.5; /* 设置合适的行高 */
  max-height: 3em; /* 两行的高度，根据实际行高调整 */
}
</style>
